﻿// 功能：
// 描述：
// 作者：温伟鹏
// 日期：2010/5/24 16:50:12

using System;
using System.Collections.Generic;
using System.Text;
using MeizuSDK.Drawing.OpenGLES;
using MeizuSDK.Presentation;
using System.Drawing;

namespace Demo
{
    class DemoEglWindow : EglWindow
    {
        OpenGLFont myHugeFont;
        OpenGLFont myFont;
        GlyphRun myLeftAligned;
        GlyphRun myCentered;
        GlyphRun myRightAligned;
        Texture myTexture;
        int[] myFrameTracker = new int[30];
        int myLastFrame = 0;
        float myFps = 0;
        float myRotation = 0;

        protected override void SetupScene()
        {
            base.SetupScene();

            myFont = new OpenGLFont(new Font(FontFamily.GenericSerif, 12, FontStyle.Regular));
            myHugeFont = new OpenGLFont(new Font(FontFamily.GenericSerif, 32, FontStyle.Regular));
            myLeftAligned = new GlyphRun(myHugeFont, "hello world", new Size(int.MaxValue, int.MaxValue), OpenGLTextAlignment.Left, true);
            myCentered = new GlyphRun(myHugeFont, "this is", new Size(Width, Height - ToolBar.HEIGHT), OpenGLTextAlignment.Center, true);
            myRightAligned = new GlyphRun(myHugeFont, "a test", new Size(Width, Height - ToolBar.HEIGHT), OpenGLTextAlignment.Right, true);
            myTexture = Texture.LoadStream(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TestOpenGLES.BitmapBrush.bmp"), false);

            myLeftAligned.ApplyTextureShader((glyphPos) =>
            {
                float left = glyphPos.TopLeft.X / myLeftAligned.Size.Width;
                float top = glyphPos.TopLeft.Y / myLeftAligned.Size.Height;
                float right = glyphPos.TopRight.X / myLeftAligned.Size.Width;
                float bottom = glyphPos.BottomLeft.Y / myLeftAligned.Size.Height;
                return new GlyphTexCoords(left, top, right, bottom);
            }
            );

            myLeftAligned.Texture = myTexture;

            myRightAligned.ApplyColorShader((glyphPos) =>
            {
                GlyphColors colors = new GlyphColors();
                colors.TopLeft = new Vector4f(1, 0, 0, 1);
                colors.BottomLeft = new Vector4f(0, 1, 0, 1);
                colors.TopRight = new Vector4f(1, 0, 0, 1);
                colors.BottomRight = new Vector4f(0, 0, 1, 1);

                return colors;
            }
            );
        }


        protected override void DrawScene()
        {
            unsafe
            {

                int count;
                //gl2.glGetIntegerv(gl.GL_MAX_TEXTURE_UNITS, &count);

                gl2.glViewport(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, ClientRectangle.Height);
                //gl.MatrixMode(gl.GL_PROJECTION);
                //gl.LoadIdentity();
                gluPerspective(45, (float)Width / ((float)Height - ToolBar.HEIGHT), .1f, 100);

                //gl.MatrixMode(gl.GL_MODELVIEW);
                //gl.LoadIdentity();

                float[] triangle = new float[] { 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f };
                float[] colors = new float[] { 1.0f, 0.0f, 0.0f, 9, 0.0f, 1.0f, 0.0f, 0, 0.0f, 0.0f, 1.0f, 0 };

                //gl.LoadIdentity();
                //gl.Translatef(0.0f, 0.0f, -6.0f);
                //gl.Rotatef(myRotation, 0.0f, 1.0f, 0.0f);

                fixed (float* trianglePointer = &triangle[0], colorPointer = &colors[0])
                {
                    //gl.EnableClientState(gl.GL_VERTEX_ARRAY);
                    //gl.VertexPointer(3, gl.GL_FLOAT, 0, (IntPtr)trianglePointer);
                    //gl.EnableClientState(gl.GL_COLOR_ARRAY);
                    //gl.ColorPointer(4, gl.GL_FLOAT, 0, (IntPtr)colorPointer);

                    gl2.glDrawArrays(gl.GL_TRIANGLES, 0, 3);
                    //gl.DisableClientState(gl.GL_VERTEX_ARRAY);
                    //gl.DisableClientState(gl.GL_COLOR_ARRAY);
                    gl2.glFlush();
                }

                myRotation += 2f;

                //gl.MatrixMode(gl.GL_PROJECTION);
                //gl.LoadIdentity();
                //gl.Orthof(0, Width, Height - ToolBar.HEIGHT, 0, -10, 10);

                //gl.MatrixMode(gl.GL_MODELVIEW);
                //gl.LoadIdentity();

                myLeftAligned.Draw();

                //gl.Translatef(0, 80, 0);
                myCentered.Draw();
                GlyphRun fpsText = new GlyphRun(myFont, Math.Round(myFps, 2).ToString() + " FPS");
                fpsText.Draw();
                //gl.Translatef(0, 80, 0);
                myRightAligned.Draw();
            }
        }

        void gluPerspective(float fovy, float aspect, float zNear, float zFar)
        {
            float xmin, xmax, ymin, ymax;

            ymax = zNear * (float)Math.Tan(fovy * 3.1415962f / 360.0);
            ymin = -ymax;
            xmin = ymin * aspect;
            xmax = ymax * aspect;

            //gl.Frustumf(xmin, xmax, ymin, ymax, zNear, zFar);
        }
    }
}
